var __HOME__= "http://"+window.location.host+"/";
/**
 * 获取地址接口
 * @param url
 * @param data
 * @param divs
 * @param form
 * @param dizhi
 */
function address_post_sub(url,data,divs,dizhi) {
    $.ajax({
        url:__HOME__+url,
        type:'POST',
        data:data,
        dataType:'json',
        success:function(data,textStatus,jqXHR){
            if(data.msg){
                layer.open({
                    content: data.msg
                    ,skin: 'msg'
                    ,time: 2
                });
            }else{
                var html = '<option value="">请选择</option>';
                $.each(data.data,function (index,element) {
                    var str = '';
                    if(dizhi != ''){
                        if(parseInt(element.id) == parseInt(dizhi)){
                            str = 'selected';
                        }
                    }
                    html = html +  '<option value="'+element.id+'" '+str+'>'+element.name+'</option>';
                });
                divs.html(html);
            }
        },
        error:function(xhr,textStatus){
            layer.open({
                content: '网络出了点小猜，请重试!'
                ,skin: 'msg'
                ,time: 2
            });
        },
    })
}


/**
 * 收货地址提交
 * @param url
 * @param data
 * @param divs
 * @param form
 * @param dizhi
 */
function info_submit() {
    var address,tel,name,sheng,shi,xian;
    tel = $.trim($("#tel").val());
    sheng = $.trim($("#shen").val());
    shi = $.trim($("#shi").val());
    xian = $.trim($("#xian").val());
    name = $.trim($("#name").val());
    address = $.trim($("#address").val());
    check_address = $.trim($("#check_address").val());
    if(address == ""){
        layer.alert('请填写地址!',{icon: 2},function(index){
            layer.close(index);
        });
        return false;
    }
    if(name==""){
        layer.alert('请填写名字!',{icon: 2},function(index){
        });
        return false;
    }
    if(tel == ""){
        layer.alert('请填写电话!',{icon: 2},function(index){
            layer.close(index);
        });
        return false;
    }
    if(tel.length!=11||isNaN(tel)||parseInt(tel)<=0){
        layer.alert('您填写的手机号码格式错误!',function(index){
            layer.close(index);
        });
        return false;
    }
    $.ajax({
        type :"POST",
        url : __HOME__+"addAddressInfo",
        data:{
            "tel":tel,
            "sheng":sheng,
            "shi":shi,
            "xian":xian,
            "name":name,
            "address":address,
            "check_address":check_address,
        },
        async : true,
        dataType:'json',
        success : function(result) {
            if(result=='1'){
                layer.alert('您的地址填写成功!',{icon: 1},function(index){
                    layer.close(index);
                    return false;
                });
            }
            window.location.href = __HOME__+"receiverAddress";
        },
        error : function() {
            alert("当前网络可能较差请稍后再试");
        }
    });
}

/**
 * 收货地址提交
 * @param url
 * @param data
 * @param divs
 * @param form
 * @param dizhi
 */
function edit_submit($address_id) {
    var address,tel,name,sheng,shi,xian;
    tel = $.trim($("#tel").val());
    sheng = $.trim($("#shen").val());
    shi = $.trim($("#shi").val());
    xian = $.trim($("#xian").val());
    name = $.trim($("#name").val());
    address = $.trim($("#address").val());
    console.log(address);
    check_address = $.trim($("#check_address").val());
    if(address == ""){
        layer.alert('请填写地址!',{icon: 2},function(index){
            layer.close(index);
        });
        return false;
    }
    if(name==""){
        layer.alert('请填写名字!',{icon: 2},function(index){
        });
        return false;
    }
    if(tel == ""){
        layer.alert('请填写电话!',{icon: 2},function(index){
            layer.close(index);
        });
        return false;
    }
    if(tel.length!=11||isNaN(tel)||parseInt(tel)<=0){
        layer.alert('您填写的手机号码格式错误!',function(index){
            layer.close(index);
        });
        return false;
    }
    $.ajax({
        type :"POST",
        url : __HOME__+"editAddressInfo",
        data:{
            "tel":tel,
            "sheng":sheng,
            "shi":shi,
            "xian":xian,
            "name":name,
            "address":address,
            "address_id":$address_id,
            "check_address":check_address,
        },
        async : true,
        dataType:'json',
        success : function(result) {
            console.log(result);
            if(result=='1'){
                layer.alert('您的地址修改成功!',{icon: 1},function(index){
                    layer.close(index);
                    return false;
                });
            }
            var index2 = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index2);
            window.location.href = __HOME__+"receiverAddress";
        },
        error : function() {
            alert("当前网络可能较差请稍后再试");
        }
    });
}

/**
 * 修改并提交用户信息
 */
function userInfo_submit(user_id) {
    var nick_name;
    nick_name = $.trim($("#nick_name").val());
    if(name==""){
        layer.alert('请填写名字!',{icon: 2},function(index){
        });
        return false;
    }
    $.ajax({
        type :"POST",
        url : __HOME__+"editUser",
        data:{
            "nick_name":nick_name,
            "user_id":user_id,
        },
        async : true,
        dataType:'json',
        success : function(result) {
            if(result=='1'){
                layer.alert('您的资料修改成功!',{icon: 1},function(index){
                    layer.close(index);
                    return false;
                });
            }
            var index2 = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index2);
            window.location.href = __HOME__+"userInfo";
        },
        error : function() {
            alert("当前网络可能较差请稍后再试");
        }
    });
}
/**
 * 订单提交接口
 * @param url
 * @param data
 * @param divs
 * @param form
 * @param dizhi
 */
function submiteOrder(){
    var tel,area,buildingType,name;
    tel= $.trim($("#tel").val());
    buildingType= $.trim($("#buildingType").val());
    area= $.trim($("#area").val());
    name= $.trim($("#name").val());
    if(tel==""){
        layer.alert('请填写电话!',{icon: 2},function(index){
            layer.close(index);
        });
    }
    if(tel.length!=11||isNaN(tel)||parseInt(tel)<=0){
        layer.alert('您填写的手机号码格式错误!',function(index){
            layer.close(index);
        });
    }
    $.ajax({
        type :"POST",
        url : __HOME__+"submitOrder",
        data:{
            "tel":tel,
            "area":area,
            "buildingType":buildingType,
            "name":name
        },
        async : true,
        dataType:'json',
        success : function(result) {
            if(result=="ok"){
                layer.alert('您的订单提交成功!',{icon: 1},function(index){
                    layer.close(index);

                });
                return false;
            }
        },
        error : function() {
            alert("当前网络可能较差请稍后再试");
        }
    });
}